Passed
Branch master (32b4c5)
by Askupa
01:33
created

Mivhak.render   B

Complexity

Conditions 2
Paths 8

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 28
rs 8.8571
1
/**
2
 * The list of registered components.
3
 * 
4
 * @type Array
5
 */
6
Mivhak.components = [];
7
8
/**
9
 * Register a new component
10
 * 
11
 * @param {string} name The components name
12
 * @param {Object} options A list of component properties
13
 */
14
Mivhak.component = function(name, options)
15
{
16
    Mivhak.components[name] = options;
17
};
18
19
/**
20
 * Render a new component
21
 * 
22
 * TODO: move this into a seperate library
23
 * 
24
 * @param {string} name The components name
25
 * @param {Object} props A list of component properties. 
26
 * This overrides the component's initial property values.
27
 */
28
Mivhak.render = function(name, props)
29
{
30
    var component = $.extend(true, {}, Mivhak.components[name]);
31
    var el = {};
32
    
33
    // Create the element from the template
34
    el.$el = $(component.template);
35
    
36
    // Create all methods
37
    $.each(component.methods, function(name, method){
38
        el[name] = function() {return method.apply(el,arguments);};
39
    });
40
    
41
    // Set properties
42
    $.each(component.props, function(name, prop){
43
        el[name] = (typeof props !== 'undefined' && props.hasOwnProperty(name) ? props[name] : prop);
44
    });
45
    
46
    // Bind events
47
    $.each(component.events, function(name, method){
48
        el.$el.on(name, function() {return method.apply(el,arguments);});
49
    });
50
    
51
    // Call the 'created' function if exists
52
    if(component.hasOwnProperty('created')) component.created.call(el);
53
    
54
    return el;
55
};